<?php
namespace app\admin\controller;
use think\Db;
use think\facade\{
    Config,Request,Cache,App
};
class Withdraw extends Base
{
    //列表
    public function lists(){
      if(is_post()){
          $d = $this->post;
          $map = [];
          if($d['status']!='') $map[] = ['and','status','=',$d['status']];
          if($d['keyword']) $map[] = ['and',"user_name|order_no","like","%{$d['keyword']}%"];
          if($d['min_time']) $map[] = ['and','create_time','>=',strtotime($d['min_time'])];
          if($d['max_time']) $map[] = ['and','create_time','<=',strtotime($d['max_time'])];
          $w  = where($map);
          $sql = "select * from withdraw  $w[0] order by status asc,id desc".limit($d['page'],$d['limit']);
          $rs = $this->db->query($sql, $w[1],'',true);
          if($rs){
              foreach ($rs[0] as $k => $v) {
                  $rs[0][$k]["status_name"] = c("type.withdraw_status.{$v['status']}");
                  $rs[0][$k]["shenhe_status_name"] = c("type.shenhe_status.{$v['shenhe_status']}");
                  $rs[0][$k]["daifa_status_name"] = c("type.daifa_status.{$v['daifa_status']}");
                  $rs[0][$k]["create_time"] = com_time_format($v['create_time']);
              }
              return suc($rs[0], $rs[1]);
          }else{
              suc([],0);
          }
      }
      $this->fetch();
    }


    public function edit(){
    	if(is_post()){
    		$d = $this->post;

            $rs = $this->db->find("withdraw",$d["id"]);
            if($rs['shenhe_status'] !=0){
                return err("该笔提现已审核");
            }else{
                if($d['daifa_type']=='1') {
                    $data = [
                        'id'=>$d["id"],
                        'shenhe_status'=>2,
                        'daifa_type'=>$d['daifa_type'],
                        'admin_user_id'=>$this->userinfo['id'],
                        'update_time'=>time()
                    ];
                    $this->db->update('withdraw',$data);

                    m('Log','xcom')->record($this->userinfo["id"], '审核通过提现订单ID['.$d["id"].']-[通联]','admin_user');
                } else {
                      $data = [
                          'id'=>$d["id"],
                          'shenhe_status'=>2,
                          'daifa_type'=>$d['daifa_type'],
                          'daifa_status'=>2,
                          'status'=>2,
                          'admin_user_id'=>$this->userinfo['id'],
                          'update_time'=>time()
                      ];
                      $this->db->update('withdraw',$data);

                      m('Log','xcom')->record($this->userinfo["id"], '审核通过提现订单ID['.$d["id"].']-[手工]','admin_user');

                      $data = [
                          "table" => "withdraw",
                          "table_id" => $d['id'],
                          "title" => "提现到账提醒",
                          "content" => '审核通过提现订单ID['.$d["id"].']-[手工]',
                          "create_time" => time(),
                      ];
                      $this->db->insert("com_message",$data);
                }
            }
            session('js_msg','审核成功');
            cookie('js_function','preload');
            return suc();
        }
        return $this->fetch(['id'=>$this->get["id"]]);
    }

    public function close(){
        if(is_post()){
            $d = $this->post;
            $rs = s("Withdraw",'c')->close($d["id"],$d["note"]);
            if($rs){
                $msg = "拒绝了提现申请，ID：" . $d["id"];
                //添加日志
                m('Log','xcom')->record($this->userinfo["id"], $msg,'admin_user');

                $data = [
                    "table" => "withdraw",
                    "table_id" => $d['id'],
                    "title" => "拒绝申请提现",
                    "content" => $msg,
                    "create_time" => time(),
                ];
                $this->db->insert("com_message",$data);

                session('js_msg','操作成功');
                cookie('js_function','preload');
                return suc();
            }else{
                return err("操作失败");
            }
        }
        return $this->fetch(['id'=>$this->get["id"]]);
    }

}
